package com.ruoyi.bussiness.controller;

import cn.hutool.core.lang.tree.Tree;
import com.ruoyi.common.vo.business.ProductCategoryVo;
import com.ruoyi.common.core.api.ApiResult;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.pagination.Paging;
import com.ruoyi.common.domain.business.ProductCategory;
import com.ruoyi.common.param.ProductCategoryParam;
import com.ruoyi.common.service.business.ProductCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 产品分类Controller
 *
 * @author Song
 * @date 2024-10-29
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/common/category")
@Api(tags = "产品分类")
public class ProductCategoryController extends BaseController {

    private final ProductCategoryService productCategoryService;

    /**
     * 查询产品分类列表
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询产品分类列表")
    public ApiResult<Paging<ProductCategoryVo>> page(ProductCategoryParam productCategoryParam) {
        Paging<ProductCategoryVo> page = productCategoryService.pageProductCategory(productCategoryParam);;
        return ApiResult.ok(page);
    }
    /**
     * 获取所有分类
     */
    @GetMapping("/list")
    @ApiOperation(value = "分页查询产品分类列表")
    public ApiResult<List<ProductCategoryVo>> getList() {
        List<ProductCategoryVo> result = productCategoryService.getList();;
        return ApiResult.ok(result);
    }
    /**
     * 获取所有分类
     */
    @GetMapping("/treeList")
    @ApiOperation(value = "分页查询产品分类列表")
    public ApiResult<List<Tree<Long>>> treeList() {
        List<Tree<Long>> trees = productCategoryService.treeList();
        return ApiResult.ok(trees);
    }
    /**
     * 新增产品分类
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增产品分类")
    public ApiResult<String> save(@RequestBody ProductCategory productCategory) {
        productCategoryService.addCategory(productCategory);;
        return ApiResult.ok("新增成功");
    }
    /**
     * 获取产品分类详情
     */
    @GetMapping("/toEdit")
    @ApiOperation(value = "获取产品分类详情")
    public ApiResult<ProductCategoryVo> toEdit(Long id) {
        ProductCategoryVo vo= productCategoryService.toEdit(id);;
        return ApiResult.ok(vo);
    }
    /**
     * 修改产品分类详情
     */
    @PostMapping("/edit")
    @ApiOperation(value = "修改产品分类详情")
    public ApiResult<String> edit(@RequestBody ProductCategory productCategory) {
        productCategoryService.edit(productCategory);;
        return ApiResult.ok("修改成功");
    }
    /**
     * 删除产品分类详情
     */
    @DeleteMapping("/remove")
    @ApiOperation(value = "删除产品分类详情")
    public ApiResult<String> remove(Long id) {
        productCategoryService.removeCategory(id);;
        return ApiResult.ok("删除成功");
    }
}
